﻿using ProductSystem.BLL.Common;
using ProductSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL
{
    public class Booking_Bll
    {
        //获取预订列表
        public static List<Booking> GetAllList()
        {
            List<Booking> list = new List<Booking>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                list = dbContext.Bookings.ToList();
            }
            return list;
        }

        /// <summary>
        /// 取消预订
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static ReturnStatus DeleteBooking(string ids,int deskID)
        {
            ReturnStatus rs;
            try
            {
                using (var dbContext = new EntitiesModel())
                {
                    string[] idList = ids.Split(',');
                    for (int i = 0; i < idList.Length; i++)
                    {
                        int id = StringExtension.GetInt(idList[i], 0);
                        Booking model = dbContext.Bookings.FirstOrDefault(o => o.ID == id);
                        model.Status = 0;
                        //dbContext.Delete(model);
                    }
                    Desk desk = dbContext.Desks.FirstOrDefault(x => x.ID == deskID);
                    desk.Status = 1;
                    dbContext.SaveChanges();
                }
                rs = PromptMessage.Instance().GetPrompt(true, TBConfig.Instance().GetPromptDeleteSuccess);
            }
            catch
            {
                rs = PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptDeleteFail);
            }
            return rs;
        }

        /// <summary>
        /// 菜谱
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, List<Food>> GetFoodList()
        {
            Dictionary<string, List<Food>> dicFood = new Dictionary<string, List<Food>>();
            List<Food> list = Food_Bll.GetAllList();
            Dictionary<int, string> dicFoodType = FoodType_Bll.GetFoodTypeDictionary();
            List<int> foodTypeIDList = list.Select(x => x.TypeID).Distinct().ToList();
            foreach (KeyValuePair<int, string> kvp in dicFoodType)
            {
                if (!dicFood.ContainsKey(kvp.Value))
                {
                    dicFood.Add(kvp.Value, list.Where(x => x.TypeID == kvp.Key).ToList());
                }
            }
            return dicFood;
        }
    }
}
